package com.university.sol.controller;

import com.github.pagehelper.PageInfo;
import com.university.sol.common.Constant;
import com.university.sol.pojo.Tag;
import com.university.sol.pojo.Team;
import com.university.sol.pojo.User;
import com.university.sol.service.TagService;
import com.university.sol.service.TeamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 侯瑞阳
 * @Date 2019/6/5 22:54
 **/
@Controller
public class PageController {
    @Autowired
    private TagService tagService;
    @Autowired
    private TeamService teamService;

    private Logger logger = LoggerFactory.getLogger(PageController.class);

    /**
     * 跳转到登录界面
     *
     * @return
     */
    @RequestMapping("page/login.html")
    public String pageLogin() {
        return "login";
    }

    /**
     * 跳转到注册界面
     */
    @RequestMapping("page/register.html")
    public String pageRegister() {
        return "register";
    }

    /**
     * 跳转到我们的团体页面
     */
    @RequestMapping("page/ourTeam.html")
    public String pageOurTeam(ModelMap modelMap, HttpServletRequest request,
                              @RequestParam(value = "tag", defaultValue = "") String tag,
                              @RequestParam(value = "pageSize", defaultValue = "9") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        if (tag.equals(",")) {
            return "404";
        }
        // 获取用户信息
        User user = (User) request.getSession(true).getAttribute(Constant.CURRENT_USER);
        // 查询标签分类
        List<Tag> tagList = tagService.getTags();
        modelMap.addAttribute("tagList", tagList);
        // 根据标签带有分页查询团级聚会
        PageInfo<List> teamList = teamService.getByEmailAndTag(user.getUserEmail(), tag, pageNum, pageSize);
        modelMap.addAttribute("teamList", teamList);
        return "ourteam";
    }

    /**
     * 跳转到团体详情页面
     */
    @RequestMapping("page/ourTeamDetail.html")
    public String pageOurTeamDetail(@RequestParam(value = "teamNum", defaultValue = "0000") String teamNum, HttpServletRequest request, ModelMap modelMap) {
        Long newTeamNum;
        try {
            newTeamNum= Long.parseLong(teamNum);
        } catch (Exception e) {
            return "404";
        }
        User user = (User) request.getSession(true).getAttribute(Constant.CURRENT_USER);
        // 根据编号邮箱号查询团体详情信息
        Team team = teamService.getByEmailAndTeamNum(user.getUserEmail(), newTeamNum);
        if (null == team) {
            return "404";
        }
        // 遍历图片
        List<String> imgs = teamService.getImgs(team.getTeamImgUrl());
        modelMap.addAttribute("imgs", imgs);
        modelMap.addAttribute("team", team);
        // 查询最近的团级项目
        List<Team> recentTeam = teamService.getByRecent(user.getUserEmail());
        modelMap.addAttribute("recentTeam", recentTeam);
        // 查询点赞数最多的团级项目
        List<Team> popularTeam = teamService.getByLikeNum(user.getUserEmail());
        modelMap.addAttribute("popularTeam", popularTeam);
        return "teamDetail";

    }
}
